<script setup>

import {ref, onMounted, watch, computed} from 'vue';
import axios from "axios";
import {snackbar} from './store'

// 查询
const pageSize = ref(10);
const pageNo = ref(1)
const headers = [
    {title: '序号', key: 'num', sortable: false},
    {title: '建筑名称', key: 'buildingId', sortable: false},
    {title: '楼层名称', key: 'floorId', sortable: false},
    {title: '点位名称', key: 'fireName', sortable: false},
    {title: '消防巡检类型', key: 'fireType', sortable: false},
    {title: '经度', key: 'longitude', sortable: false},
    {title: '纬度', key: 'latitude', sortable: false},
    {title: '二维码', key: 'fireImg', sortable: false},
    {title: '负责人', key: 'userId', sortable: false},
    {title: '操作', key: 'actions', sortable: false}
];
const items = ref([]);
const loading = ref(true);
const totalItems = ref(0);
const buildingList = ref([]);
const floorAllList = ref([]);
const floorList = ref([]);
const floorSelectList = ref([]);
const userList = ref([]);
const imgBaseUrl = ref("http://172.168.180.4:900/");

const search = ref('');
const name = ref(null);
const buildingName = ref(null);
const floorName = ref(null);
watch(name, () => {
    search.value = name.value
})
watch(floorName, () => {
    search.value = floorName.value
})
watch(buildingName, () => {
    search.value = buildingName.value
    axios.post("/checkFireFloor/select",{
        buildingId: buildingName.value,
        pageNo:1,
        pageSize: 100
    }).then(res => {
        floorSelectList.value = res.data.data.records;
    })
})

function changeFloor() {
    axios.post("/checkFireFloor/select", {
        buildingId: editedItem.value.buildingId,
        pageNo: 1,
        pageSize: 100
    }).then(res => {
        floorList.value = res.data.data.records;
    })
}

function loadItems({page, itemsPerPage}) {
    loading.value = true;
    pageNo.value = page
    pageSize.value = pageSize
    axios.post('/checkFireHydrant/select', {
        pageNo: page,
        pageSize: itemsPerPage,
        fireName: name.value,
        buildingId: buildingName.value,
        floorId: floorName.value
    }).then(({data}) => {
        items.value = data.data.records;
        totalItems.value = data.data.total;
        loading.value = false;
    })
}

onMounted(() => {
    loadItems({page: 1, itemsPerPage: pageSize.value});
    axios.post('/checkFireBuilding/getAll', {}).then(({data}) => {
        buildingList.value = data.data;
    })
    axios.post('/checkFireFloor/getAll', {}).then(({data}) => {
        floorAllList.value = data.data;
    })
    axios.post("/user/selectPage", {pageNo:1, pageSize: 10000, delFlag: "0"}).then(res => {
        userList.value = res.data.data.records
    })
});

// 增改
const isShowDialog = ref(false)
const dialogEnum = {
    update: '编辑',
    create: '新增'
}
const dialogType = ref(null)
const isShowDeleteDialog = ref(false)
const editedItem = ref({
    fireName: ""
});

function resetEditedItem() {
    editedItem.value = {
        fireName: ""
    }
}

function clickNew() {
    dialogType.value = dialogEnum.create
    isShowDialog.value = true
    resetEditedItem()
}

function close() {
    isShowDialog.value = false
    resetEditedItem()
    dialogType.value = null
}

function clickEdit(item) {
    editedItem.value = Object.assign({}, item)
    changeFloor()
    editedItem.value.fireType += ""
    isShowDialog.value = true
    dialogType.value = dialogEnum.update
}

function save() {
    if (dialogType.value === dialogEnum.update) {
        axios({
            method: 'POST',
            url: '/checkFireHydrant/update',
            data: editedItem.value
        }).then(() => {
            snackbar.info('编辑成功')
            close()
            loadItems({page: pageNo.value, itemsPerPage: pageSize.value})
        })
    } else if (dialogType.value === dialogEnum.create) {
        axios({
            method: 'POST',
            url: '/checkFireHydrant/save',
            data: editedItem.value
        }).then((res) => {
            console.log(res.data)
            snackbar.info('创建成功')
            close()
            loadItems({page: pageNo.value, itemsPerPage: pageSize.value})
        })
    }
}

// 删
const beDeleteItem = ref({})

function clickDelete(item) {
    isShowDeleteDialog.value = true
    beDeleteItem.value = item
}

function clickDeleteConfirm() {
    axios.get('/sys_role/role/delRole/?roleId=' + beDeleteItem.value.id).then(() => {
        snackbar.info("删除成功")
        loadItems({page: 1, itemsPerPage: pageSize.value})
        clickCloseDelete()
    })
}

function clickCloseDelete() {
    isShowDeleteDialog.value = false
    beDeleteItem.value = null
}

</script>

<template>
    <v-toolbar density="compact">
        <v-toolbar-title>点位管理</v-toolbar-title>
        <v-select v-model="buildingName" :items="buildingList" item-title="buildingName" item-value="id" label="建筑名称" :clearable="true" hide-details placeholder="建筑名称"></v-select>
        <v-select v-model="floorName" :items="floorSelectList" item-title="floorName" item-value="id" label="楼层名称" :clearable="true" hide-details placeholder="楼层名称"></v-select>
        <v-text-field v-model="name" :clearable="true" hide-details placeholder="查询名称"></v-text-field>
        <v-dialog v-model="isShowDialog" max-width="500px">
            <template v-slot:activator="{ props }">
                <v-btn icon="mdi-plus" v-on:click="clickNew" v-bind="props"></v-btn>
            </template>
            <v-card>
                <v-card-title>{{ dialogType }}</v-card-title>
                <v-card-text>
                    <v-text-field v-model="editedItem.fireName" label="点位名称" required></v-text-field>
                    <v-select
                    v-model="editedItem.buildingId"
                    :items="buildingList"
                    @update:model-value="changeFloor"
                    item-title="buildingName"
                    item-value="id"
                    label="建筑名称"
                    placeholder="建筑名称"></v-select>
                    <v-select
                    v-model="editedItem.floorId"
                    :items="floorList"
                    item-title="floorName"
                    item-value="id"
                    label="楼层名称"
                    placeholder="楼层名称"></v-select>
                    <v-select v-model="editedItem.fireType" :items="[{value:'0',title:'消火栓'},{value:'1',title:'灭火器'}]"
                    label="消防巡检类型"></v-select>
                    <v-text-field v-model="editedItem.longitude" label="经度" required></v-text-field>
                    <v-text-field v-model="editedItem.latitude" label="纬度" required></v-text-field>
                    <v-autocomplete
                    v-model="editedItem.userId"
                    :items="userList"
                    filterable
                    item-title="nickname"
                    item-value="id"
                    label="负责人"
                    placeholder="负责人"></v-autocomplete>
                </v-card-text>
                <v-card-actions>
                    <v-btn color="primary" variant="text" @click="close">关闭</v-btn>
                    <v-btn color="primary" variant="text" @click="save">确定</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <v-dialog v-model="isShowDeleteDialog" max-width="500px">
            <v-card>
                <v-card-title class="text-h5">确定删除?</v-card-title>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="blue-darken-1" variant="text" @click="clickCloseDelete">取消</v-btn>
                    <v-btn color="blue-darken-1" variant="text" @click="clickDeleteConfirm">确定</v-btn>
                    <v-spacer></v-spacer>
                </v-card-actions>
            </v-card>
        </v-dialog>
    </v-toolbar>
  <!--  分页文本待更换   items-per-page-text="每页数量"-->
    <v-data-table-server :search="search" :items-per-page="pageSize" :headers="headers" :items-length="totalItems"
    :items="items"
    :loading="loading"
    class="elevation-1"
    items-per-page-text="每页数量"
    @update:options="loadItems">
        <template v-slot:item.num="{ index }">
            <span>{{ index + 1 }}</span>
        </template>
        <template v-slot:item.status="{ item }">
            <span v-if="item.raw.status == 0">正常</span>
            <span v-if="item.raw.status == 1">停用</span>
        </template>
        <template v-slot:item.buildingId="{ item }">
            <span v-for="k in buildingList" v-show="k.id == item.raw.buildingId">{{ k.buildingName }}</span>
        </template>
        <template v-slot:item.floorId="{ item }">
            <span v-for="k in floorAllList" v-show="k.id == item.raw.floorId">{{ k.floorName }}</span>
        </template>
        <template v-slot:item.fireType="{ item }">
            <span v-if="item.raw.fireType == 0">消火栓</span>
            <span v-if="item.raw.fireType == 1">灭火器</span>
        </template>
        <template v-slot:item.userId="{ item }">
            <span v-for="k in userList" v-show="k.id == item.raw.userId">{{ k.nickname }}</span>
        </template>
        <template v-slot:item.fireImg="{ item }">
            <el-image :src="imgBaseUrl + item.raw.fireImg" :preview-src-list="[imgBaseUrl + item.raw.fireImg]" style="height: 50px;" alt=""></el-image>
        </template>
        <template v-slot:item.actions="{ item }">
            <v-btn icon="mdi-pencil" variant="text" @click="clickEdit(item.raw)"></v-btn>
            <!-- <v-btn icon="mdi-delete" variant="text" @click="clickDelete(item.raw)"></v-btn> -->
        </template>
    </v-data-table-server>
</template>

<style scoped lang="less">

</style>
